package com.third.clock.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.lzy.okgo.model.Response;
import com.third.clock.MainActivity;
import com.third.clock.R;
import com.third.clock.bean.LoginEntity;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.Contants;
import com.third.clock.common.LogUtils;
import com.third.clock.common.SharedPreferencesUtils;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TimerManager;
import com.third.clock.manager.TransformManager;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class WelcomeActivity extends Activity{

    private static final String TAG="WelcomeActivity";

    private ClockManager mClockManager;

    private SharedPreferencesUtils mSharedPreferencesUtils;

    private Handler handler=new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                login();
            }
        },2000);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    private void login(){
        mClockManager=ClockManager.getInstace(WelcomeActivity.this);
        mSharedPreferencesUtils=SharedPreferencesUtils.getInstance(WelcomeActivity.this);
        String password = mSharedPreferencesUtils.getStringValue(SharedPreferencesUtils.PASSWORD_KEY,"");
        String username = mSharedPreferencesUtils.getStringValue(SharedPreferencesUtils.USERNAME_KTY,"");
        if(mClockManager!=null&& !TextUtils.isEmpty(password)&&!TextUtils.isEmpty(username)){
            handler.postDelayed(loginTimeOut,1000*3);
            mClockManager.login(new OkHttpClientManager.okHttpCallback() {
                @Override
                public void onFailure(Response<String> s) {
                    handler.removeCallbacks(loginTimeOut);
                    startLoginActivity();
                }

                @Override
                public void onResponse(Response<String> response) {
                    handler.removeCallbacks(loginTimeOut);
                    if(response!=null&&response.body()!=null){
                        try {
                            ResponseData<LoginEntity> mLoginResponseData= TransformManager.paserLogin(response.body());
                            if (mLoginResponseData != null&&mLoginResponseData.getCode() == Contants.CODE_0) {
                                startMainActivity();
                            }else{
                                startLoginActivity();
                            }
                        }catch (Exception e){
                            startLoginActivity();
                        }
                    }else{
                        startLoginActivity();
                    }
                }
            });
        }else{
            startLoginActivity();
        }
    }

    private void startLoginActivity(){
        if(!WelcomeActivity.this.isFinishing()) {
            WelcomeActivity.this.startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
        }
        this.finish();
    }

    private void startMainActivity(){
        if(!WelcomeActivity.this.isFinishing()) {
            WelcomeActivity.this.startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
        }
        this.finish();
    }

    private Runnable loginTimeOut=new Runnable() {
        @Override
        public void run() {
            startLoginActivity();
        }
    };

}
